package com.tcat.item.service;


import com.tcat.common.enums.ExceptionEnum;
import com.tcat.common.exception.TcatException;
import com.tcat.item.mapper.*;
import com.tcat.item.pojo.SpecGroup;
import com.tcat.item.pojo.SpecParam;
import com.tcat.item.pojo.Specification;
import com.tcat.item.pojo.Stock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author liys
 * @Date 2021/1/10
 * @Version 1.0.0
 **/
@Service
public class SpecificationService {
    @Autowired
    private SpuMapper spuMapper;
    @Autowired
    private SkuMapper skuMapper;
    @Autowired
    private StockMapper stockMapper;
    @Autowired
    private SpecGroupMapper specGroupMapper;

    @Autowired
    private SpecParamMapper specParamMapper;

    @Autowired
    private SpecificationMapper specificationMapper;


    /**
     * 根据id查询规格参数模板
     * @param id
     * @return
     */
    public Specification queryById(Long id) {
        return this.specificationMapper.selectByPrimaryKey(id);
    }


    /**
     * 删除规格参数模板
     * @param specification
     */
    public void deleteSpecification(Specification specification) {
        this.specificationMapper.deleteByPrimaryKey(specification);
    }


    /**
     * 保存规格参数模板
     * @param specification
     */
    public void saveSpecification(Specification specification) {
        this.specificationMapper.insert(specification);
        //String str=specification.getSpecifications();
        //String[] res=str.split(",");
        //String[] res1=res[0].split(":");
        //String groupName=res1[1].substring(1,res1.length-1);
        //String[] res2=res[1].split(":");
        //SpecGroup specGroup=new SpecGroup();
        //specGroup.setName(groupName);
        //saveSpecGroup(specGroup);
    }

    /**
     * 修改规格参数模板
     * @param specification
     */
    public void updateSpecification(Specification specification) {
        this.specificationMapper.updateByPrimaryKeySelective(specification);
    }




    /**
     * 根据分类id查询组
     * @param cid
     * @return
     */
    public List<SpecGroup> querySpecGroupByCid(Long cid) {
        //查询条件
        SpecGroup group = new SpecGroup();
        group.setCid(cid);
        //查询
        List<SpecGroup> list = specGroupMapper.select(group);
        if(CollectionUtils.isEmpty(list)){
            //没查到
            throw new TcatException(ExceptionEnum.SPEC_GROUP_NOT_FOUND);
        }

        return list;
    }


    /**
     * 保存组
     * @param specGroup
     */
    public void saveSpecGroup(SpecGroup specGroup) {
        int count=this.specGroupMapper.insert(specGroup);
        if(count!=1){
            throw new TcatException(ExceptionEnum.BRAND_SAVE_ERROR);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteSpecGroup(Long id) {
        if (id == null) {
            throw new TcatException(ExceptionEnum.INVALID_FILE_TYPE);
        }
        SpecGroup specGroup = new SpecGroup();
        specGroup.setId(id);
        int count = specGroupMapper.deleteByPrimaryKey(specGroup);
        if (count != 1) {
            throw new TcatException(ExceptionEnum.DELETE_SPEC_GROUP_FAILED);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateSpecGroup(SpecGroup specGroup) {
        int count = specGroupMapper.updateByPrimaryKey(specGroup);
        if (count != 1) {
            throw new TcatException(ExceptionEnum.UPDATE_SPEC_GROUP_FAILED);
        }
    }







    /**
     * 根据条件查询规格参数
     * @param gid
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public List<SpecParam> queryParams(Long gid,Long cid,Boolean searching) {
        SpecParam record=new SpecParam();
        record.setGroupId(gid);
        record.setCid(cid);
        record.setSearching(searching);
        List<SpecParam> list=specParamMapper.select(record);
        if(CollectionUtils.isEmpty(list)){
            throw new TcatException(ExceptionEnum.SPEC_GROUP_NOT_FOUND);
        }
        return list;
    }



    @Transactional(rollbackFor = Exception.class)
    public void saveSpecParam(SpecParam specParam) {
        specParam.setId(null);
        int count = specParamMapper.insert(specParam);
        if (count != 1) {
            throw new TcatException(ExceptionEnum.SPEC_PARAM_CREATE_FAILED);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateSpecParam(SpecParam specParam) {
        int count = specParamMapper.updateByPrimaryKeySelective(specParam);
        if (count != 1) {
            throw new TcatException(ExceptionEnum.UPDATE_SPEC_PARAM_FAILED);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteSpecParam(Long id) {
        if (id == null) {
            throw new TcatException(ExceptionEnum.INVALID_PARAM);
        }
        int count = specParamMapper.deleteByPrimaryKey(id);
        if (count != 1) {
            throw new TcatException(ExceptionEnum.DELETE_SPEC_PARAM_FAILED);
        }
    }





    public List<SpecParam> querySpecParamList(Long gid, Long cid, Boolean searching) {
        SpecParam param=new SpecParam();
        param.setGroupId(gid);
        param.setCid(cid);
        param.setSearching(searching);

        List<SpecParam> list = specParamMapper.select(param);
        if(CollectionUtils.isEmpty(list)){
            throw  new TcatException(ExceptionEnum.SPEC_PARAM_NOT_FOUND);
        }
        return list;
    }

    public List<SpecGroup> queryListByCid(Long cid) {
        // 查询规格组
        List<SpecGroup> specGroups = querySpecGroupByCid(cid);
        // 查询当前分类下的参数
        List<SpecParam> specParams = querySpecParamList(null, cid,  null);
        //先把规格参数变成map，map的key是规格组id,map的值是组下的所有参数
        Map<Long, List<SpecParam>> map = new HashMap<>();

        for (SpecParam param : specParams) {
            if (!map.containsKey(param.getGroupId())) {
                //这个组id在map中不存在，新增一个list
                map.put(param.getGroupId(), new ArrayList<>());
            }
            map.get(param.getGroupId()).add(param);

        }
        //填充param到group
        for (SpecGroup specGroup : specGroups) {
            specGroup.setParams(map.get(specGroup.getId()));
        }

        return specGroups;
    }


    public int countSkus(){
        return skuMapper.selectAll().size();
    }

    public Stock queryStock(Long id){
         Stock stock=stockMapper.selectByPrimaryKey(id);
         return stock;
    }

}
